//index.js
var base64 = require("../../images/base64");
//获取应用实例
const app = getApp()

Page({
    data: {
        banners: [
            'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3495514660,2986887290&fm=26&gp=0.jpg',
            'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2929458141,3230547596&fm=200&gp=0.jpg',
            'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3301034990,2876024777&fm=26&gp=0.jpg'
        ],
        bulletin:{},
        tabs: ["帮我买", "帮我送", "帮我排队"],
        navBuy:[
            {attrstatus:'1', title:'来包烟',type:'icon-xiangyan'},
            {attrstatus:'1', title:"大药房",type:'icon-yaofang'},
            {attrstatus:'1', title:'便利店',type:'icon-tubiaozhizuomoban-'},
            {attrstatus:'1', title:'买小吃',type:'icon-xiaochi'}
        ],
        navDeliver:[
            {attrstatus:'2', title:'送朵花',type:'icon-huadian-baihe'},
            {attrstatus:'2', title:"蛋糕店",type:'icon-hezi'},
            {attrstatus:'2', title:'送水果',type:'icon-shuiguo'}
        ],
        navRow:[
            {attrstatus:'3', title:'医院挂号',type:'icon-yiyuan'},
            {attrstatus:'3', title:"入学排队",type:'icon-dingdan'},
            {attrstatus:'3', title:'取餐排队',type:'icon-paiduiquhao0101'}
        ],
        activeIndex: 0,
        sliderOffset: 0,
        sliderLeft: 0,
        sliderWidth: 0,
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        icon: base64.icon20,
        userType:0,
        runOrderLists:[],
        uId:'',
        isLogin:true,
    },
    onLoad: function () {
        this.getUserInfo();
        this.getBanners();
        this.getBulletin();
    },
    onShow:function(){
        var userType = wx.getStorageSync('userType');
        var uId = wx.getStorageSync('u_id');;
        if(!userType){
            this.getRect();
            this.setData({
                userType:0,
                uId:uId
            })
        }else{
            this.setData({
                userType:1,
                uId:uId
            })
            this.getRunOrderLists();
        }
    },
    getUserInfo:function(){
        var that = this;
        //获取用户信息授权
        wx.getSetting({
            success(res) {
                if (!res.authSetting['scope.userInfo']) {
                    wx.authorize({
                        scope: 'scope.userInfo',
                        success(resInfo) {
                            that.wxGetUserInfo();
                        },
                        fail: function (res) {
                            that.authorize();
                        }
                    })
                } else {
                    that.wxGetUserInfo();
                }
            },
            fail: function (data) {
                // that.getUserInfo();
            }
        })
    },
    wxGetUserInfo:function(){
        var that = this;
        wx.getUserInfo({
            success: function (resInfo) {
                that.login({detail:resInfo});
            },
        })
    },
    authorize: function () {
        var that = this;
        wx.showModal({
            title: '打开设置页面进行授权',
            content: '需要获取您的公开信息（昵称、头像等），请到小程序的设置中打开用户信息授权',
            cancelText: '取消',
            confirmText: '去设置',
            success: function (res) {
                if (res.confirm) {
                    wx.openSetting({
                        success: (res) => {
                            if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
                                that.getUserInfo();
                            }
                        }
                    })

                } else {
                    that.getUserInfo();
                }
            },
            fail: function (res) {
                that.getUserInfo();
            }
        })
    },
    login:function (e) {
        var that = this;
        if(that.data.isClick) return;
        that.data.isClick = 1;
        wx.showToast({
            icon:'loading',
            title:'加载中'
        })
        app.util.setUserInfo(e);
        wx.login({//调用登录获取code换取openId和sessionkey
            success: function (res) {
                var code = res.code;
                if (code){
                    var data = {
                        code:code,
                    }
                    app.util.request({
                        url: app.globalRequestUrl + '/return_id',
                        data: data,
                        success: function (res) {
                            if (res && res.msg == 'success') {
                                var info = app.util.getUserInfo();
                                if(info){
                                    that.loginUid({
                                        nick_name:info.nickName,
                                        avatarurl:info.avatarUrl,
                                        openid:res.data
                                    });
                                }
                            }
                        },
                        complete: function () {
                            // console.log(app.util.getUserInfo());
                        }
                    });

                }else {
                    console.log('获取用户登录态失败！' + res.msg)
                }
            }
        });
    },
    loginUid:function (data) {
        var that = this;
        app.util.request({
            url: app.globalRequestUrl + '/getmation',
            data: data,
            success: function (res) {
                if (res && res.data) {
                    var info = app.util.getUserInfo();
                    that.setData({
                        isLogin:true,
                        userInfo: info
                    });
                    app.util.setCookies(res.data)
                }
            },
            complete: function () {
                wx.hideToast();
            }
        });
    },
    getRect: function () {
        var that = this;
        setTimeout(function () {
            wx.createSelectorQuery().select('#slider-item-0').boundingClientRect(function (rect) {
                that.setData({
                    sliderWidth: rect.width
                })
            }).exec();
        },10)
    },
    tabClick: function (e) {
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: e.currentTarget.dataset.index
        });
    },
    getBanners:function () {
        var that = this;
        app.util.request({
            url:app.globalRequestUrl + '/banner',
            success:function (res) {
                if(res.data){
                    that.setData({
                        banners:res.data
                    })
                }
            },
            fail:function () {
                
            },
            complete:function () {
                
            }

        })
    },
    getBulletin:function () {
        var that = this;
        app.util.request({
            url:app.globalRequestUrl + '/bulletin',
            success:function (res) {
                if(res.data){
                    that.setData({
                        bulletin:res.data
                    })
                }
            },
            fail:function () {

            },
            complete:function () {

            }

        })
    },
    getRunOrderLists:function (index) {
        var that = this;
        wx.showToast({
            icon:'loading',
            title:'加载中'
        })
        app.util.request({
            url:app.globalRequestUrl + '/runorderlists',
            method:'GET',
            success:function (res) {
                if(res && res.msg == 'success'){
                    var lists = [];
                    for (var i=0;i<res.data.length;i++){
                        if(res.data[i].u_id != that.data.uId){
                            lists.push(res.data[i]);
                        }
                    }
                    that.setData({
                        runOrderLists:lists
                    })
                }
            },
            complete:()=>  {
                wx.hideToast()
            }
        })
    },
    bindbulletin:function () {
        var content = this.data.bulletin.content;
        if(!content) return;
        wx.showModal({
            content: content,
            showCancel: false,
            confirmText:'知道了',
            success: function (res) {
                if (res.confirm) {
                    console.log('用户点击确定')
                }
            }
        });
    }
})
